package com.wf.course.web.controller.goods;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.utils.export.GoodsConfigExcelExporter;
import com.utils.export.GoodsInfoOrderExcelExporter;
import com.utils.export.GoodsOrderExcelExporter;
import com.utils.export.GoodsSalesExcelExporter;
import com.utils.page.PageBean;
import com.wf.course.model.ad.VipCard;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.model.course.Speaker;
import com.wf.course.model.goods.GoodsInfo;
import com.wf.course.model.goods.GoodsOrder;
import com.wf.course.model.goods.GoodsOrderRelation;
import com.wf.course.model.member.CoinLog;
import com.wf.course.model.member.GiveCoinLog;
import com.wf.course.model.user.User;
import com.wf.course.model.user.UserRoleRelation;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IGoodsInfoService;
import com.wf.course.service.IGoodsOrderRelationService;
import com.wf.course.service.IGoodsOrderService;
import com.wf.course.service.ISpeakerService;
import com.wf.course.service.IUserRoleRelationService;
import com.wf.course.service.IVipCardService;
import com.wf.course.web.Constants;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 订单信息
 * @content 列表Controller
 */
@RequestMapping("goodsOrder")
@Controller
public class GoodsOrderController {

	@Autowired
	private IGoodsOrderService goodsOrderService;
	@Autowired
	private IGoodsOrderRelationService orlService;
	@Autowired
	private ICourseInfoService courseInfoService;
	@Autowired
	private ICourseChapterService chapterService;
	@Autowired
	private IVipCardService cardService;
	@Autowired
	private IGoodsInfoService goodsInfoService;
	@Autowired
	private ISpeakerService ispeakerservice;
	@Autowired
	private IUserRoleRelationService userRoleRelationService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param orderCode
	 * @param model
	 * @return
	 */
	@LogInfo("查询商品订单信息列表")
	@RequestMapping("goodsOrderList")
	public String goodsOrderList(HttpSession httpSession,
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "orderCode", required = false) String orderCode, 
			@RequestParam(value = "orderStatus", required = false) String orderStatus, 
			@RequestParam(value = "userSource", required = false) String userSource, 
			@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
			@RequestParam(value = "orderEndTime", required = false) String orderEndTime, 
			@RequestParam(value = "telephone", required = false) String telephone, 
			@RequestParam(value = "statusList", required = false) String  statusList,
			@RequestParam(value = "backFlag",required = false) String backFlag,
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		if(orderCode!=null && orderCode!=""){
			cons.put("orderCode", orderCode);
		}
		if(orderStatus!=null && orderStatus!=""){
			
			String[] strs = orderStatus.split(",");
			List<String> statusList1 = Arrays.asList(strs);
			 statusList1 = Arrays.asList(strs);
			
			cons.put("statusList", statusList1);
			
		}else if(statusList!=null && statusList!=""){
			
			statusList = statusList.replace("[", "").replace("]", "");
			String[] strs = statusList.split(",");
			List<String> statusList2 = Arrays.asList(strs);
			statusList2 = Arrays.asList(strs);
			
			cons.put("statusList", statusList2);
		}
		
		if(userSource!=null && userSource!=""){
			cons.put("userSource", userSource);
		}
		if(orderBeginTime!=null && orderBeginTime!=""){
			cons.put("orderBeginTime", orderBeginTime);
		}
		if(orderEndTime!=null && orderEndTime!=""){
			cons.put("orderEndTime", orderEndTime);
		}
		if(telephone!=null && telephone!=""){
			cons.put("telephone", telephone);
		}
		if (StringUtils.isNotEmpty(backFlag)) {
			cons.put("backFlag", backFlag);
		}
		
		PageBean<GoodsOrder> pageBean = new PageBean<GoodsOrder>(pageNo, pageSize, null, cons);
		
		pageBean.setUrl("goodsOrder/goodsOrderList");
		this.goodsOrderService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		
		Object token = httpSession.getAttribute("login_token");
		User user = Constants.LOGIN_USER.get(token.toString());
		if(user != null){
			model.addAttribute("loginUser", user.getUserName());
			 
		}
		
		//查询用户角色:userRole
		List<UserRoleRelation>	userRoleRelations = userRoleRelationService.getByUserID(user.getId());
		if(userRoleRelations!=null && userRoleRelations.size()>0){
			model.addAttribute("roleName",  userRoleRelations.get(0).getRoleName());
		}
		
		
		return "goods/goodsOrderList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * @param orderCode
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{orderCode}_{id}")
	public String downLoadById(@PathVariable("orderCode") String orderCode, 
			@PathVariable("id") String id,
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			Model model) {
		
		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("orderCode", orderCode);
		PageBean<GoodsOrderRelation> pageBean = new PageBean<GoodsOrderRelation>(pageNo, pageSize, null, cons);
		/*PageBean<GoodsOrder> pageBean = new PageBean<GoodsOrder>(pageNo, pageSize, null, cons);
		
		pageBean.setUrl("goodsOrder/pageLoad"+orderCode);*/
		GoodsOrder goodsOrder=	this.goodsOrderService.queryGoodsOrderByOrderCodes(orderCode);
		
		if(StringUtils.isEmpty(goodsOrder.getVersion())){
			goodsOrder.setOldCopperCash(goodsOrder.getSpendCoin());	
		}else if(goodsOrder.getVersion().equals("2.0")){
			goodsOrder.setOldCopperCash(goodsOrder.getSpendCoin());
		}else if(goodsOrder.getVersion().equals("3.0")){
			goodsOrder.setOldCopperCash(goodsOrder.getOldCopperCash());
		} 
 		model.addAttribute("goodsOrder", goodsOrder);
 		List<GoodsOrder> listName=this.goodsOrderService.getGoodsNames(orderCode);
		String goodsNames = "";
		if(listName!=null &&listName.size()>0 && listName.get(0) != null){
			
			for(GoodsOrder gor : listName){
				goodsNames+=gor.getGoodsNames() +"\n";
				
			}
		}
				
 		model.addAttribute("goodsNames", goodsNames);
 		/*GoodsOrder goodsOrder = this.goodsOrderService.queryGoodsOrderByOrderCode(orderCode);
 		model.addAttribute("goodsOrder", goodsOrder);*/
 		pageBean.setUrl("goodsOrder/pageLoad"+orderCode);
		this.orlService.indexInfo(pageBean);
		
 		model.addAttribute("page", pageBean);
 		model.addAttribute("source", id);//跳转路径
		return "goods/goodsOrderEdit";
	}

	@ResponseBody
	@LogInfo("查询订单详细信息")
	@RequestMapping(value="queryOrderDetails",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
	public String queryOrderDetails(@RequestParam("orderCode") String orderCode){
		List<GoodsOrderRelation> list  = orlService.queryOrderGoodsListByOrderCode(orderCode);
		
		return new Gson().toJson(list);
	}
	
	/**
	   * 订单导出，导出为excel文件
	   * 订单包括课程和商品
	   * @return
	   */
  @LogInfo("导出excel商品订单信息")
  @RequestMapping(value="exportExcel"  , method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
  public void exportExcel(HttpServletRequest request, 
		    @RequestParam(value = "orderCode", required = false) String orderCode, 
			@RequestParam(value = "orderStatus", required = false) String orderStatus, 
			@RequestParam(value = "userSource", required = false) String userSource, 
			@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
			@RequestParam(value = "orderEndTime", required = false) String orderEndTime,
			@RequestParam(value = "telephone", required = false) String telephone, 
			HttpServletResponse response,
			Model model){
           
	      	// 组装一个查询条件的map
	  		Map<String, Object> cons = new HashMap<String, Object>();
	  		if(orderCode!=null && orderCode!=""){
	  			cons.put("orderCode", orderCode);
	  		}
	  		if(orderStatus!=null && orderStatus!=""){
	  			String[] strs = orderStatus.split(",");
				List<String> statusList = Arrays.asList(strs);
				cons.put("statusList", statusList);
	  		}
	  		if(userSource!=null && userSource!=""){
	  			cons.put("userSource", userSource);
	  		}
	  		if(orderBeginTime!=null && orderBeginTime!=""){
	  			cons.put("orderBeginTime", orderBeginTime);
	  		}
	  		if(orderEndTime!=null && orderEndTime!=""){
	  			cons.put("orderEndTime", orderEndTime);
	  		}
	  		if(telephone!=null && telephone!=""){
				cons.put("telephone", telephone);
			}
	  		PageBean<GoodsOrder> pageBean = new PageBean<GoodsOrder>(null, null, null, cons);	  		
	  		 
	  		List<GoodsOrder> goodsOrderList = this.goodsOrderService.goodOrders(pageBean);
	  		/*List<GoodsOrder> goodsOrderLists =new ArrayList<GoodsOrder>()*/;
	  		if(goodsOrderList!=null && goodsOrderList.size()>0){
	  			

  				//遍历订单，取出订单中的商品信息
  				for(int i=0;i<goodsOrderList.size();i++){
  					
  				//订单总额
  				//	Double orderAMount = Double.parseDouble( goodsOrderList.get(i).getGoodsAmount());
  					
  					List<GoodsOrderRelation> goodsInfos = this.orlService.queryOrderGoodsList(goodsOrderList.get(i).getOrderCode());
  					if(goodsInfos!=null && goodsInfos.size()>0){
  	  					goodsOrderList.get(i).setOrderDetailList(goodsInfos);
  					}
  					
  					
  					
  				if(goodsInfos!=null && goodsInfos.size()>0){
  						//遍历商品列表，取商品信息
  						StringBuffer goodsInfoStr = new StringBuffer();
  						//商品总金额
  						BigDecimal orderGoodsTotalPrice =new BigDecimal(0);
  						//课程总金额
  						BigDecimal CourseInfoAmount = new BigDecimal(0);
  						//课程详情
  						StringBuffer  courseInfoStr = new StringBuffer();
  						for(int k=0;k<goodsInfos.size();k++){
  							/*GoodsOrder goodsOrder = new GoodsOrder();
  							goodsOrder.setOldCopperCash(goodsInfos.get(k).getOldCopperCash());
  							goodsOrder.setNewCopperCash(goodsInfos.get(k).getNewCopperCash());
  							goodsOrder.setGold(goodsInfos.get(k).getGold());*/
  							/*goodsOrderLists.add(goodsOrder);*/
  							//计算订单中商品的总金额，和商品信息
  							if(goodsInfos.get(k).getGoodsSort()==4){
  								//取出订单中的同一商品价格：
  								BigDecimal goodsPrice =new BigDecimal(0);
 								 if(!"".equals(goodsInfos.get(k).getGoodsPrice()) &&goodsInfos.get(k).getGoodsPrice()!=null ){
 									 goodsPrice = goodsInfos.get(k).getGoodsPrice();
 								 }
  								
  								//取出订单中同一商品的数量
  								int goodsCount =0;
  								if(goodsInfos.get(k).getGoodsCount()!=null ){
  									goodsCount = goodsInfos.get(k).getGoodsCount();
 								 }
  								//计算订单中同一商品的总额
  								BigDecimal goodsTotalCount = goodsPrice.multiply(new BigDecimal(goodsCount));
  								//一个订单中所有商品的价格
  								orderGoodsTotalPrice=orderGoodsTotalPrice.add(goodsTotalCount);
  								//订单中的商品详情
  								 StringBuffer goodsInfo =new StringBuffer();
  								goodsInfo.append("【商品名称】：").append(goodsInfos.get(k).getGoodsName())
  										.append("【商品数量】：").append(goodsInfos.get(k).getGoodsCount())
  										.append("【商品规格】：").append(goodsInfos.get(k).getSpecification())
  										.append("【商品单价】：").append(goodsInfos.get(k).getGoodsPrice());
  								 
  								goodsInfoStr=goodsInfoStr.append(goodsInfo).append("\r\n");
  								
  								
  								
  								
  								
  							}else{
  								
  								//取出订单中的同一课程价格：
  								 
  								 
  								BigDecimal goodsPrice =new BigDecimal(0);
  								 if(!"".equals(goodsInfos.get(k).getGoodsPrice()) &&goodsInfos.get(k).getGoodsPrice()!=null ){
  									 goodsPrice = goodsInfos.get(k).getGoodsPrice();
  								 }
  								
  								//取出订单中同一课程的数量
  								int goodsCount =0;
  								if(goodsInfos.get(k).getGoodsCount()!=null ){
  									goodsCount = goodsInfos.get(k).getGoodsCount();
 								 }
  								//计算订单中同一课程的总额
  								BigDecimal goodsTotalCount = goodsPrice.multiply(new BigDecimal(goodsCount));
  								//一个订单中所有课程的价格
  								CourseInfoAmount=CourseInfoAmount.add(goodsTotalCount);
  								//订单中的课程详情
  								 StringBuffer courseInfo =new StringBuffer();
  								courseInfo.append("【课程名称】：").append(goodsInfos.get(k).getGoodsName())
  										.append("【课程单价】:").append(goodsInfos.get(k).getGoodsPrice());
  								
  								courseInfoStr = courseInfoStr.append(courseInfo).append("\r\n");
   							}
  							//goodsOrderList.get(i).setOldCopperCashs(goodsInfos.get(k).getCouponsCash());
  							
  						}
  						 
   						
  						//将订单中的商品详情，保存
  						goodsOrderList.get(i).setGoodsDetailStr(goodsInfoStr.toString());
  						//将订单中，商品总金额，保存
  						goodsOrderList.get(i).setGoodsInfoAmount( orderGoodsTotalPrice);
  						//将订单中的课程详情，保存
  						goodsOrderList.get(i).setCourseDetailStr(courseInfoStr.toString());
  						//将订单中，课程总金额，保存
  						goodsOrderList.get(i).setCourseInfoAmount(CourseInfoAmount);
  						if(goodsOrderList.get(i).getVersion()==null || "".equals(goodsOrderList.get(i).getVersion())){
  							 //将订单中的原铜钱，保存
  	  						goodsOrderList.get(i).setOldCopperCash(goodsOrderList.get(i).getSpendCoin());
  						}else if(goodsOrderList.get(i).getVersion().equals("2.0")){
  						//将订单中的原铜钱，保存
  	  						goodsOrderList.get(i).setOldCopperCash(goodsOrderList.get(i).getSpendCoin());
  							
  						}else if (goodsOrderList.get(i).getVersion().equals("3.0")){
  							
  							 //将订单中的原铜钱，保存
  	  						goodsOrderList.get(i).setOldCopperCash(goodsOrderList.get(i).getOldCopperCash());	
  							
  						}
  					  
  					  //将订单中的新铜钱，保存
  						goodsOrderList.get(i).setNewCopperCash(goodsOrderList.get(i).getNewCopperCash());
  					   //将订单中的使用金币，保存
  						goodsOrderList.get(i).setGold(goodsOrderList.get(i).getGold());
  						 //订单总金额
  						goodsOrderList.get(i).setGoodsAmount(goodsOrderList.get(i).getGoodsAmount());
  						 //商品实际支付
  						goodsOrderList.get(i).setOrderAmount(goodsOrderList.get(i).getOrderAmount());
  						
  						//保存订单标识到表格中
  						 String orderPayTime=goodsOrderList.get(i).getOrderPayTime();
  						String orderPagTimes="2017-09-29 00:00:00";
  						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  						try {
  							if(orderPayTime!=null){
							if(sdf.parse(orderPayTime).getTime()>sdf.parse(orderPagTimes).getTime()){
						      String identification ="新订单";
							 goodsOrderList.get(i).setIdentification(identification);
							}else{
								String identification="旧订单";
								goodsOrderList.get(i).setIdentification(identification);
							}
  							}
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
  					
  						/*goodsOrderLists.add(goodsOrderList.get(i));*/
  						
  						String realpays=goodsOrderList.get(i).getRealPay();
  						String realpay="0.00";
  						if(realpays!=null){
  						if(realpays.indexOf('-')!=-1){
  							goodsOrderList.get(i).setRealPay(realpay);	
  						}else{
  							goodsOrderList.get(i).setRealPay(goodsOrderList.get(i).getRealPay());	
  						}
  						}else{
  							
  							goodsOrderList.get(i).setRealPay(realpay);		
  						}
  					}
  				}
  			// 实例化表以Excel格式的导出工具
  			GoodsOrderExcelExporter dosExcelExporter = new GoodsOrderExcelExporter();
  			// 执行导出操作
				dosExcelExporter.exportExcel(response, goodsOrderList);
			 
  		
	  			
	  		}    
		
  	}
    
	/**
   * 订单导出，导出为excel文件
   * 过滤掉课程，只留商品
   * @return
   */
 @LogInfo("导出excel商品订单信息（只包含商品）")
@RequestMapping(value="exportGoodsInfoExcel"  , method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
public void exportGoodsInfoExcel(HttpServletRequest request, 
	    @RequestParam(value = "orderCode", required = false) String orderCode, 
		@RequestParam(value = "orderStatus", required = false) String orderStatus, 
		@RequestParam(value = "userSource", required = false) String userSource, 
		@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
		@RequestParam(value = "orderEndTime", required = false) String orderEndTime,
		@RequestParam(value = "telephone", required = false) String telephone, 
		HttpServletResponse response,
		Model model){
       
	 	// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		if(orderCode!=null && orderCode!=""){
			cons.put("orderCode", orderCode);
		}
		if(orderStatus!=null && orderStatus!=""){
			String[] strs = orderStatus.split(",");
			List<String> statusList = Arrays.asList(strs);
			cons.put("statusList", statusList);
		}
		if(userSource!=null && userSource!=""){
			cons.put("userSource", userSource);
		}
		if(orderBeginTime!=null && orderBeginTime!=""){
			cons.put("orderBeginTime", orderBeginTime);
		}
		if(orderEndTime!=null && orderEndTime!=""){
			cons.put("orderEndTime", orderEndTime);
		}
		if(telephone!=null && telephone!=""){
			cons.put("telephone", telephone);
		}
		PageBean<GoodsOrder> pageBean = new PageBean<GoodsOrder>(null, null, null, cons);	  		
		 
		List<GoodsOrder> goodsOrderList = this.goodsOrderService.goodOrders(pageBean);
		
		if(goodsOrderList!=null && goodsOrderList.size()>0){
			

			//遍历订单，取出订单中的商品信息
			for(int i=0;i<goodsOrderList.size();i++){
			//订单总额
			//	Double orderAMount = Double.parseDouble( goodsOrderList.get(i).getGoodsAmount());
				
				List<GoodsOrderRelation> goodsInfos = this.orlService.queryOrderGoodsList(goodsOrderList.get(i).getOrderCode());
				if(goodsInfos!=null && goodsInfos.size()>0){
					//遍历商品列表，取商品信息
					StringBuffer goodsInfoStr = new StringBuffer();
					//商品总金额
					BigDecimal orderGoodsTotalPrice =new BigDecimal(0);
					//课程总金额
					BigDecimal CourseInfoAmount = new BigDecimal(0);
					//课程详情
					StringBuffer  courseInfoStr = new StringBuffer();
					for(int k=0;k<goodsInfos.size();k++){
						//计算订单中商品的总金额，和商品信息
						if(goodsInfos.get(k).getGoodsSort()==4){
							//取出订单中的同一商品价格：
							BigDecimal goodsPrice =new BigDecimal(0);
							 if(!"".equals(goodsInfos.get(k).getGoodsPrice()) &&goodsInfos.get(k).getGoodsPrice()!=null ){
								 goodsPrice = goodsInfos.get(k).getGoodsPrice();
							 }
							
							//取出订单中同一商品的数量
							int goodsCount =0;
							if(goodsInfos.get(k).getGoodsCount()!=null ){
								goodsCount = goodsInfos.get(k).getGoodsCount();
							 }
							//计算订单中同一商品的总额
							BigDecimal goodsTotalCount = goodsPrice.multiply(new BigDecimal(goodsCount));
							//一个订单中所有商品的价格
							orderGoodsTotalPrice=orderGoodsTotalPrice.add(goodsTotalCount);
							//订单中的商品详情
							 StringBuffer goodsInfo =new StringBuffer();
							goodsInfo.append("【商品名称】：").append(goodsInfos.get(k).getGoodsName())
									.append("【商品数量】：").append(goodsInfos.get(k).getGoodsCount())
									.append("【商品规格】：").append(goodsInfos.get(k).getSpecification())
									.append("【商品单价】：").append(goodsInfos.get(k).getGoodsPrice());
							 
							goodsInfoStr=goodsInfoStr.append(goodsInfo).append("\r\n");
							
						}else{
							
							//取出订单中的同一课程价格：
							 
							 
							BigDecimal goodsPrice =new BigDecimal(0);
							 if(!"".equals(goodsInfos.get(k).getGoodsPrice()) &&goodsInfos.get(k).getGoodsPrice()!=null ){
								 goodsPrice = goodsInfos.get(k).getGoodsPrice();
							 }
							
							//取出订单中同一课程的数量
							int goodsCount =0;
							if(goodsInfos.get(k).getGoodsCount()!=null ){
								goodsCount = goodsInfos.get(k).getGoodsCount();
							 }
							//计算订单中同一课程的总额
							BigDecimal goodsTotalCount = goodsPrice.multiply(new BigDecimal(goodsCount));
							//一个订单中所有课程的价格
							CourseInfoAmount=CourseInfoAmount.add(goodsTotalCount);
							//订单中的课程详情
							 StringBuffer courseInfo =new StringBuffer();
							courseInfo.append("【课程名称】：").append(goodsInfos.get(k).getGoodsName())
									.append("【课程单价】:").append(goodsInfos.get(k).getGoodsPrice());
							
							courseInfoStr = courseInfoStr.append(courseInfo).append("\r\n");
						}
						
					}
					 
					
					//将订单中的商品详情，保存
					goodsOrderList.get(i).setGoodsDetailStr(goodsInfoStr.toString());
					//将订单中，商品总金额，保存
					goodsOrderList.get(i).setGoodsInfoAmount( orderGoodsTotalPrice);
					//将订单中的课程详情，保存
					goodsOrderList.get(i).setCourseDetailStr(courseInfoStr.toString());
					//将订单中，课程总金额，保存
					goodsOrderList.get(i).setCourseInfoAmount(CourseInfoAmount);
				 
				}
			}
		// 实例化表以Excel格式的导出工具
		GoodsInfoOrderExcelExporter dosExcelExporter = new GoodsInfoOrderExcelExporter();
		// 执行导出操作
			dosExcelExporter.exportExcel(response, goodsOrderList);
		 
	
			
		} 
	
	}

/**
 * 订单统计
 * @param userSource
 * @param orderBeginTime
 * @param orderEndTime
 * @param model
 * @return
 */
 @LogInfo("统计订单销售额")
@RequestMapping("goodsOrderCount")
public String goodsOrderCount(@RequestParam(value = "userSource", required = false) String userSource, 
		@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
		@RequestParam(value = "orderEndTime", required = false) String orderEndTime, 
		Model model) {
	//课程销售总额
	 
	BigDecimal big1 = new BigDecimal(0);
	//商品销售总额
	BigDecimal big2 = new BigDecimal(0);
	GoodsOrderRelation orderRelation = new GoodsOrderRelation();
	//课程中的销售额累加
	List<GoodsOrderRelation> courseAmounts = this.orlService.getCourseAmount(userSource, orderBeginTime, orderEndTime);
 	 if(courseAmounts!=null && courseAmounts.size()>0){
 		 for(int i=0;i<courseAmounts.size();i++){
 			GoodsOrderRelation gor =  courseAmounts.get(i);
 			if(gor!=null){
 				
 				big1=big1.add(new BigDecimal(gor.getCourseAmount()));
 				 
 			}
 			
 		 }
 		orderRelation.setCourseAmount(big1.toString());
 	 	 
 	 }
 	 
 	 //商品总额累加
 	List<GoodsOrderRelation> goodsAmounts = this.orlService.getGoodsAmount(userSource, orderBeginTime, orderEndTime);
 	 if(goodsAmounts!=null&& goodsAmounts.size()>0){
 		for(int i=0;i<goodsAmounts.size();i++){
  			GoodsOrderRelation gor =  goodsAmounts.get(i);
 			if(gor!=null){
 				 
 				big2 = big2.add(new BigDecimal(gor.getGoodsAmount()));
 			}
 			
 		 }
 		orderRelation.setGoodsAmount(big2.toString());
 		 
 	 }
	 
 	 	//计算销售总额
  	 	orderRelation.setSaleAmount(big1.add(big2).toString());
  	 	
  	  //如果查询条件不为空，将查询条件保存到对象
 	 	if(orderBeginTime!=null && orderBeginTime!=""){
 	 		orderRelation.setOrderBeginTime(orderBeginTime);
  		}
 	 	if(orderEndTime!=null && orderEndTime!=""){
 	 		orderRelation.setOrderEndTime(orderEndTime);
  		}
 	 	if(userSource!=null && userSource!=""){
 	 		orderRelation.setUserSource(userSource);
  		}
	
	model.addAttribute("orderRelation", orderRelation);
	
	return "goods/goodsOrderCount";
}
 

	/**
	 * 删除订单,同时删除订单关联的所有商品
	 * @param id
	 * @return
	 */
	@LogInfo("删除订单及其关联所有商品")
	@RequestMapping(value = "remove/{orderCode}")
	public String removeGoodsOrder(@PathVariable("orderCode") String orderCode) {
		 
		 
		goodsOrderService.deleteByOrderCode(orderCode);
				
		orlService.deleteByOrderCode(orderCode);
		
		return "redirect:/goodsOrder/goodsOrderList";
	}
	
	
	/**
	 * 加载订单配置列表查询页面
	 * @param pageNo
	 * @param pageSize
	 * @param telephone
	 * @return
	 */
 	@RequestMapping(value = "goodsOrderConfigList" , produces = "text/html;charset=UTF-8")
	public String goodsOrderConfigList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "telephone", required = false) String telephone, 
			@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
			@RequestParam(value = "orderEndTime", required = false) String orderEndTime, 
			Model model) {
 		
 		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		if(telephone!=null && telephone!=""){
			cons.put("telephone", telephone);
		}
		if(orderBeginTime!=null && orderBeginTime!=""){
			cons.put("orderBeginTime", orderBeginTime);
		}
		if(orderEndTime!=null && orderEndTime!=""){
			cons.put("orderEndTime", orderEndTime);
		}
		PageBean<GoodsOrder> pageBean = new PageBean<GoodsOrder>(pageNo, pageSize, null, cons);
		
		pageBean.setUrl("goodsOrder/goodsOrderConfigList");
		this.goodsOrderService.getConfigOrders(pageBean);
		
		model.addAttribute("page", pageBean);
 		
		
 		return "goods/goodsOrderConfigList";
	}
	/**
	 * 加载订单配置页面
	 * @param goodsOrder
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "goodsOrderConfigLoad" , produces = "text/html;charset=UTF-8")
	public String goodsOrderConfigLoad(@ModelAttribute GoodsOrder goodsOrder, HttpServletRequest request, Model model) {
		
		 List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		 
		 List<CourseChapter> chapters = chapterService.queryCourseChapters();
		 model.addAttribute("chapters",chapters);
		 
		 List<VipCard> cards = cardService.queryVipCards();
		 model.addAttribute("cards",cards);
		 
		/* List<GoodsInfo> goods = goodsInfoService.queryGoods();
		 model.addAttribute("goods",goods);*/
		
		 return "goods/goodsOrderConfig";
	}
	/**
	 * 配置订单信息
	 * @param goodsOrder
	 * @param request
	 * @return
	 */
	@LogInfo("配置订单信息")
	@RequestMapping(value = "goodsOrderConfig" , produces = "text/html;charset=UTF-8")
	public String goodsOrderConfig(@ModelAttribute GoodsOrder goodsOrder,
			@ModelAttribute GoodsOrderRelation goodsOrderRelation,
			@ModelAttribute GiveCoinLog coinLog,
			HttpServletRequest request) {
		
		goodsOrderService.configGoodsOrder(goodsOrder,goodsOrderRelation,coinLog);
		
		return "redirect:/goodsOrder/goodsOrderConfigList";
	}
	/**
	 * 订单统计
	 * @param pageNo
	 * @param pageSize
	 * @param userSource
	 * @param goodsSort
	 * @param orderBeginTime
	 * @param orderEndTime
	 * @param model
	 * @return
	 */
	 @LogInfo("按商品统计销售额")
	@RequestMapping("getGoodsSale")
	public String getGoodsSale() {
		 
			return "goods/goodsSaleList";
	}

	/**
	 * 订单统计
	 *
	 * @param pageNo
	 * @param pageSize
	 * @param userSource
	 * @param goodsSort
	 * @param orderBeginTime
	 * @param orderEndTime
	 * @param model
	 * @return
	 */
	@LogInfo("按商品统计销售额")
	@RequestMapping("getGoodsSaleSearch")
	public String getGoodsSaleSearch(Model model,
									 @RequestParam(value = "pageNo", required = false) Integer pageNo,
									 @RequestParam(value = "pageSize", required = false) Integer pageSize,
									 @RequestParam(value = "userSource", required = false) String userSource,
									 @RequestParam(value = "goodsSort", required = false) String goodsSort,
									 @RequestParam(value = "orderBeginTime", required = false) String orderBeginTime,
									 @RequestParam(value = "orderEndTime", required = false) String orderEndTime,
									 @RequestParam(value = "speakerName", required = false) String speakerName) {
		Map<String, Object> map = new HashMap<>();

		if (StringUtils.isNotBlank(userSource)) {
			map.put("userSource", userSource);
		}
		if (StringUtils.isNotBlank(goodsSort)) {
			map.put("goodsSort", goodsSort);
		}
		if (StringUtils.isNotBlank(orderBeginTime)) {
			map.put("orderBeginTime", orderBeginTime);
		}
		if (StringUtils.isNotBlank(orderEndTime)) {
			map.put("orderEndTime", orderEndTime);
		}
		if (StringUtils.isNotBlank(speakerName)) {
			map.put("speakerName", speakerName);
		}
		PageBean<GoodsOrderRelation> pageBean = new PageBean<>(pageNo, pageSize, null, map);
		pageBean.setUrl("goodsOrder/getGoodsSaleSearch");
		orlService.newGetGoodsSale(pageBean);
		model.addAttribute("teacher", ispeakerservice.queryteacher());
		model.addAttribute("page", pageBean);

		return "goods/goodsSaleList";
	}
	 
	 
		/**
	   * 销售额导出，导出为excel文件
	   * @return
	   */
	 @LogInfo("销售额导出excel")
	@RequestMapping(value="exportGoodsSaleExcel"  , method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public void exportGoodsSaleExcel(HttpServletRequest request, 
			@RequestParam(value = "userSource", required = false) String userSource, 
			@RequestParam(value = "goodsSort", required = false) String goodsSort, 
			@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
			@RequestParam(value = "orderEndTime", required = false) String orderEndTime, 
			@RequestParam(value = "speakerName", required = false) String speakerName, 
			HttpServletResponse response,
			Model model){
	       
		 	// 组装一个查询条件的map
			Map<String, Object> cons = new HashMap<String, Object>();
			if(speakerName!=null && speakerName!=""){
				cons.put("speakerName", speakerName);
				String goodsSorts="1";
				cons.put("goodsSort", goodsSorts);
			}
			if(userSource!=null && userSource!=""){
				cons.put("userSource", userSource);
			}
			if(goodsSort!=null && goodsSort!="" && goodsSort!="1"){
				cons.put("goodsSort", goodsSort);
			}
			if(orderBeginTime!=null && orderBeginTime!=""){
				cons.put("orderBeginTime", orderBeginTime);
			}
			if(orderEndTime!=null && orderEndTime!=""){
				cons.put("orderEndTime", orderEndTime);
			}
			
			PageBean<GoodsOrderRelation> pageBean = new PageBean<GoodsOrderRelation>(null, null, null, cons);	  		
			 
			List<GoodsOrderRelation> goodSalesList = this.orlService.exportGoodSales(pageBean);
			
			// 实例化表以Excel格式的导出工具
			GoodsSalesExcelExporter dosExcelExporter = new GoodsSalesExcelExporter();
			// 执行导出操作
				dosExcelExporter.exportExcel(response, goodSalesList);
			 
		
				
			 
		
		}
	 
	 
	 
	 /**
	  * 订单统计
	  * @param model
	  * @return
	  */
	  @LogInfo("订单支付统计")
	 @RequestMapping("goodsTotalAmount")
	 public String goodsTotalAmount(@RequestParam(value = "userSource", required = false) String userSource, 
				@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
				@RequestParam(value = "orderEndTime", required = false) String orderEndTime, 
	 		Model model) {
	 	GoodsOrder goodsOrder = goodsOrderService.goodsTotalAmount(userSource, orderBeginTime, orderEndTime);
	 	//将查询条件值，一起返回
	 	if(goodsOrder!=null){
	 	goodsOrder.setOrderBeginTime(orderBeginTime);
	 	goodsOrder.setOrderEndTime(orderEndTime);
	 	goodsOrder.setUserSource(userSource);
	 	model.addAttribute("goodsOrder", goodsOrder);
	 	}
	 	return "goods/goodsTotalAmount";
	 }
	  
	  
	  
	  /**
	   * 配置订单导出
	   * @return
	   */
	@LogInfo("配置订单导出")
	@RequestMapping(value="goodsOrderConfigExportExcel"  , method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public void goodsOrderConfigExportExcel(HttpServletRequest request, 
			@RequestParam(value = "telephone", required = false) String telephone, 
			@RequestParam(value = "orderBeginTime", required = false) String orderBeginTime, 
			@RequestParam(value = "orderEndTime", required = false) String orderEndTime, 
			HttpServletResponse response,
			Model model){
         
			// 组装一个查询条件的map
			Map<String, Object> cons = new HashMap<String, Object>();
			 
			if(telephone!=null && telephone!=""){
				cons.put("telephone", telephone);
			}
			if(orderBeginTime!=null && orderBeginTime!=""){
				cons.put("orderBeginTime", orderBeginTime);
			}
			if(orderEndTime!=null && orderEndTime!=""){
				cons.put("orderEndTime", orderEndTime);
			}
			 
	  		PageBean<GoodsOrder> pageBean = new PageBean<GoodsOrder>(null, null, null, cons);	  		
	  		 
	  		List<GoodsOrder> orders = this.goodsOrderService.exportConfigGoodsOrder(pageBean);
	  		
	  		if(orders!=null && orders.size()>0){
	  			
				//遍历订单，取出订单中的商品信息
				for(int i=0;i<orders.size();i++){
					
					List<GoodsOrderRelation> goodsInfos = this.orlService.queryOrderGoodsList(orders.get(i).getOrderCode());
					if(goodsInfos!=null && goodsInfos.size()>0){
						if(goodsInfos.get(0)!= null){
							orders.get(i).setCourseDetailStr(goodsInfos.get(0).getGoodsName());
						}
						
					}
					
				}
			// 实例化表以Excel格式的导出工具
			GoodsConfigExcelExporter dosExcelExporter = new GoodsConfigExcelExporter();
			// 执行导出操作
				dosExcelExporter.exportExcel(response, orders);
			 
		
	  			
	  		}    
	   }
	 
	/**
	 * 订单退货
	 * @param orderCode
	 * @return
	 */
	@LogInfo("订单退货")
	@RequestMapping("goodsReturn/{orderCode}")
	public String goodsReturn(@PathVariable("orderCode") String orderCode){
		goodsOrderService.goodsReturn(orderCode);
		
		return "redirect:/goodsOrder/goodsOrderList";
	}
	
	
	 @RequestMapping(value="updateOrderRemark"  , method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	  public String updateOrderRemark(HttpServletRequest request, 
			    @RequestParam(value = "orderCode", required = false) String orderCode, 
				@RequestParam(value = "uqdateremark", required = false) String uqdateremark,
				@RequestParam(value = "pageNo", required = false) Integer pageNo,
				@RequestParam(value = "pageSize", required = false) Integer pageSize,
				HttpServletResponse response,
				Model model){
		 
		 goodsOrderService.goodsupdateOrderRemark(orderCode,uqdateremark);
		// 组装一个查询条件的map
			Map<String, Object> cons = new HashMap<String, Object>();
			cons.put("orderCode", orderCode);
			PageBean<GoodsOrderRelation> pageBean = new PageBean<GoodsOrderRelation>(pageNo, pageSize, null, cons);
			/*PageBean<GoodsOrder> pageBean = new PageBean<GoodsOrder>(pageNo, pageSize, null, cons);
			
			pageBean.setUrl("goodsOrder/pageLoad"+orderCode);*/
			GoodsOrder goodsOrder=	this.goodsOrderService.queryGoodsOrderByOrderCodes(orderCode);
			/*this.orlService.indexInfo(pageBean);
			*/
	 		model.addAttribute("goodsOrder", goodsOrder);
	 		List<GoodsOrder> listName=this.goodsOrderService.getGoodsNames(orderCode);
			String goodsNames = "";
			if(listName!=null &&listName.size()>0){
				
				for(GoodsOrder gor : listName){
					goodsNames+=gor.getGoodsNames() +"\n";
					
				}
			}
					
	 		model.addAttribute("goodsNames", goodsNames);
	 		/*GoodsOrder goodsOrder = this.goodsOrderService.queryGoodsOrderByOrderCode(orderCode);
	 		model.addAttribute("goodsOrder", goodsOrder);*/
	 		pageBean.setUrl("goodsOrder/pageLoad"+orderCode);
			this.orlService.indexInfo(pageBean);
			
	 		model.addAttribute("page", pageBean);
	 		model.addAttribute("source", 1);//跳转路径
			return "goods/goodsOrderEdit";
		 
	 }
	/**
	 * 修改订单备注信息
	 * @param orderCode
	 * @return
	 *//*
	@LogInfo("订单退货")
	@RequestMapping("updateOrderRemark/{orderCode}")
	public String updateOrderRemark(
			@PathVariable("orderCode") String orderCode,
			@RequestParam(value = "uqdateremark", required = false) String uqdateremark
			){
		goodsOrderService.goodsupdateOrderRemark(orderCode,uqdateremark);
		
		return "goods/goodsOrderEdit";
	}*/

	/**
	 * 销售额导出，导出为excel文件(New)
	 */
	@LogInfo("销售额导出excel")
	@RequestMapping(value = "newExportGoodsSaleExcel", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public void exportGoodsSaleExcel(HttpServletResponse response,
									 @RequestParam(value = "goodsSort", required = false) String goodsSort,
									 @RequestParam(value = "userSource", required = false) String userSource,
									 @RequestParam(value = "orderBeginTime", required = false) String orderBeginTime,
									 @RequestParam(value = "orderEndTime", required = false) String orderEndTime,
									 @RequestParam(value = "speakerName", required = false) String speakerName) {

		List<GoodsOrderRelation> goodSalesList = this.orlService.newExportGoodSales(StringUtils.isNotBlank(goodsSort) ?
				Integer.valueOf(goodsSort) : null, userSource, orderBeginTime, orderEndTime, speakerName);

		new GoodsSalesExcelExporter().newExportExcel(response, goodSalesList);
	}


	/**
	 * 用户购买金额导出excel
	 */
	@LogInfo("用户购买金额导出excel")
	@RequestMapping(value = "exportPurchaseAmountExcel", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public void exportPurchaseAmountExcel(HttpServletResponse response,
										  @RequestParam(value = "beginTime", required = false) String beginTime,
										  @RequestParam(value = "endTime", required = false) String endTime) {

		List<GoodsOrderRelation> purchaseAmountList = orlService.exportPurchaseAmountExcel(beginTime, endTime);

		new GoodsSalesExcelExporter().exportPurchaseAmountExcel(response, purchaseAmountList);
	}

	@LogInfo("用户购买金额列表")
	@RequestMapping("purchaseAmountList")
	public String purchaseAmountList(Model model, Integer pageNo, Integer pageSize, String beginTime, String endTime) {
		Map<String, Object> map = new HashMap<>();

		if (StringUtils.isNotBlank(beginTime)) {
			map.put("beginTime", beginTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			map.put("endTime", endTime);
		}

		PageBean<GoodsOrderRelation> pageBean = new PageBean<>(pageNo, pageSize, null, map);
		pageBean.setUrl("goodsOrder/purchaseAmountList");

		orlService.purchaseAmountList(pageBean);

		model.addAttribute("page", pageBean);

		return "goods/purchaseAmountList";
	}
}

